package com.itchen.hospital.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.PatientAttendanceMapper;
import com.itchen.hospital.mapper.PatientMapper;
import com.itchen.hospital.mapper.UserMapper;
import com.itchen.hospital.pojo.*;
import com.itchen.hospital.service.PatientService;
import com.itchen.hospital.service.UserService;
import com.itchen.hospital.utils.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private PatientAttendanceMapper patientAttendanceMapper;

    //条件查询
    @Override
    public PageInfo<Patient> search(Params params) {
        //处理分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //获取登录人的姓名
        Admin currentUser = JwtTokenUtils.getCurrentUser();
        if (currentUser.getRole().equals("DOCTOR")){
            params.setDoctorName(currentUser.getUsername());
        }
        if (currentUser.getRole().equals("USER")){
            params.setPatientName(currentUser.getUsername());
        }
        List<Patient> list = patientMapper.findBySearch(params);
        return PageInfo.of(list);
    }


    //修改
    @Override
    public void update(Patient patient) {
        //挂号之后，将信息存入就诊表
        Patient patientInfo = patientMapper.findById(patient.getId());
        if (patientInfo != null && !"1".equals(patientInfo.getStatus())) {
            PatientAttendance patientAttendance = new PatientAttendance();
            patientAttendance.setPatientName(patientInfo.getPatientName());
            patientAttendance.setDoctorName(patientInfo.getDoctorName());
            patientAttendance.setAppointmentTime(patientInfo.getAppointmentTime());
            patientAttendanceMapper.insert(patientAttendance);
        }
        //挂号
        patient.setStatus("1");
        patientMapper.update(patient);

    }

}
